<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">
                首页</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/admin/list' }">
                基础管理</el-breadcrumb-item
            >
            <el-breadcrumb-item :to="{ path: '/admin/list' }"
                >账号列表</el-breadcrumb-item
            >
            <el-breadcrumb-item>编辑管理账号</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <el-page-header @back="goBack" content="编辑管理员账号">
            </el-page-header>
            <el-form
                :model="editForm"
                :rules="editFormRules"
                ref="editFormRef"
                label-width="80px"
                hide-required-asterisk
            >
                <el-form-item label="账号" prop="account">
                    <el-input
                        v-model="editForm.account"
                        :disabled="editForm.id == 1"
                    >
                    </el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="editForm.name"> </el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="editForm.email"> </el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="editForm.mobile"> </el-input>
                </el-form-item>
                <el-form-item label="状态" align="left">
                    <el-switch
                        v-model="editForm.status"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="subEdit">保存</el-button>
                    <el-button @click="goBack()">返回</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
import requestApis from '@/request'
import validator from '@/validator/index.js'
export default {
    data () {
        return {
            editForm: {
                id: 0,
                account: '',
                name: '',
                email: '',
                mobile: '',
                status: 1
            },
            // 添加表单验证规则对象
            editFormRules: {
                account: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 2, max: 20, message: '账号长度在2~20位之间', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                    { min: 2, max: 6, message: '账号长度在2~6位之间', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { min: 6, max: 34, message: '账号长度在6~34位之间', trigger: 'blur' },
                    { validator: validator.checkEmail, trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { validator: validator.checkMobile, trigger: 'blur' }
                ]
            }
        }
    },
    created () {
        this.getRow(this.$route.query.id)
    },
    methods: {
        goBack () {
            this.$router.back()
        },
        getRow (id) {
            requestApis.getAdminRow({ id }).then(res => {
                if (res.meta.code === 1) {
                    this.editForm.id = res.data.id
                    this.editForm.account = res.data.account
                    this.editForm.name = res.data.name
                    this.editForm.email = res.data.email
                    this.editForm.mobile = res.data.mobile
                    this.editForm.status = res.data.status
                } else {
                    this.$message({
                        message: res.meta.msg,
                        type: 'warning'
                    })
                    this.$router.push('/admin')
                }
            }).catch(error => {
                console.log(error)
            })
        },
        subEdit () {
            this.$refs.editFormRef.validate(valid => {
                if (!valid) {
                    return false
                }
                requestApis.saveAdminEdit(this.editForm).then(res => {
                    if (res.meta.code === 1) {
                        this.$message({
                            message: res.meta.msg,
                            type: 'success'
                        })
                        this.$router.back()
                    } else {
                        this.$message({
                            message: res.meta.msg,
                            type: 'warning'
                        })
                    }
                }).catch(error => {
                    console.log(error)
                })
            })
        }
    }
}
</script>

<style>
.el-form {
    width: 60%;
    margin-top: 20px;
}
</style>
